{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 运行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "from tianyancha import Tianyancha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "# 单个\n",
    "table_dict = Tianyancha('13488895246', 'abcd1234').tianyancha_scraper('中信证券股份有限公司',table=['baseInfo','staff'], export='json')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "# 批量\n",
    "tuple_dicts = Tianyancha('13488895246', 'abcd1234').tianyancha_scraper_batch()\n",
    "tuple_dicts[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模拟登陆：Cookies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f = open('cookies.txt', 'r')\n",
    "dic_cookies = {}\n",
    "for line in f:\n",
    "    print (line)\n",
    "    listedline = line.split('\t') # split around the = sign\n",
    "    if len(listedline) > 1: # we have the = sign in there\n",
    "    dic_cookies[listedline[0]] = listedline[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv('cookies_2.txt', header=None, sep='\\t')\n",
    "df.columns = ['domain', 'httpOnly', 'path', 'secure', 'expiry', 'name', 'value']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "df.to_json('cookies.txt', orient='records')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "from fake_useragent import UserAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "UserAgent(cache=False, use_cache_server=False, verify_ssl=False)#.chrome"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 模拟登陆：GUI自动化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "import pyautogui"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "inputHidden": false,
    "outputHidden": false
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "\n",
    "# 防止用户已经将选项卡切换到了\"密码登录\"使得login_option.png因为下方出现蓝色小条而无法匹配，使用Try-Except提高程序稳健性\n",
    "try:\n",
    "    x, y = pyautogui.locateCenterOnScreen('login_option.png')\n",
    "    print (x, y)\n",
    "    pyautogui.click(x, y)\n",
    "except:\n",
    "    pass\n",
    "    \n",
    "x, y = pyautogui.locateCenterOnScreen('login_id.png')\n",
    "print (x, y)\n",
    "pyautogui.click(x, y)\n",
    "pyautogui.typewrite(self.username)\n",
    "\n",
    "x, y = pyautogui.locateCenterOnScreen('login_password.png')\n",
    "print (x, y)\n",
    "pyautogui.click(x, y)\n",
    "pyautogui.typewrite(self.password)\n",
    "\n",
    "x, y = pyautogui.locateCenterOnScreen('login_button.png')\n",
    "print (x, y)\n",
    "pyautogui.click(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tianyancha.tianyancha import Tianyancha"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernel_info": {
   "name": "python3"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "nteract": {
   "version": "0.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
